/*
 * Copyright (c) 2018 - 2028. www.vtuzx.com Inc. All Rights Reserved
 */
package com.vtuzx.kingdee.xqc_api.web;

import com.vtuzx.core.VtuzxException;
import com.vtuzx.core.bean.dto.VtuzxFile;
import com.vtuzx.core.bean.dto.VtuzxToken;
import com.vtuzx.core.constant.IVtuzxConst;
import com.vtuzx.core.utils.VtuzxUtil;
import com.vtuzx.kingdee.xqc_api.service.IXqcOtherService;
import com.vtuzx.web.constant.ApiType;
import com.vtuzx.web.validator.VtuzxValidator;
import com.vtuzx.web.web.VtuzxApi;
import com.vtuzx.web.web.VtuzxControlParam;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

/**
 * 采购收货
 */
@RequestMapping("/${vtuzx.fast.api.path}/${vtuzx.fast.api.version}/other_col")
@RestController
public class XqcOtherColController {

    /** 采购服务 */
    @Autowired
    private IXqcOtherService otherService;

    /**
     * 保存其他入库单
     */
    @PostMapping("/upload_othercol_bill")
    @VtuzxApi
    public Object uploadOtherColBill(@VtuzxControlParam VtuzxToken token, @RequestParam Map<String, Object> param, @RequestParam("file") MultipartFile[] files) throws Exception {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String name = VtuzxUtil.getString(token, "name");
        List<VtuzxFile> vFileList = VtuzxUtil.getObject(param, "files");
        otherService.uploadOtherColBill(_userId, name, vFileList.get(0).file());
        return null;
    }

    /**
     * 保存其他入库单
     */
    @PostMapping("/save_othercol_bill")
    @VtuzxApi( fields = {
            @VtuzxValidator(key = "materialNo", label = "物料", notNull = true),
            @VtuzxValidator(key = "batchNo", label = "批号", notNull = true),
            @VtuzxValidator(key = "produceDate", label = "生产日期", notNull = true),
            @VtuzxValidator(key = "otherCount", label = "总重", notNull = true),
    })
    public Object saveOtherColBill(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws VtuzxException {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String name = VtuzxUtil.getString(token, "name");
        return otherService.saveOtherColBill(_userId, name, param);
    }

    /**
     * 保存其他入库单
     */
    @PostMapping("/delete_othercol_bill")
    @VtuzxApi( fields = {
            @VtuzxValidator(key = "billNo", label = "单号", notNull = true)
    } )
    public Object deleteOtherColBill(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws VtuzxException {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String billNo = VtuzxUtil.getString(param, "billNo");
        otherService.deleteOtherColBill(_userId, billNo);
        return null;
    }

    /**
     * 审核其他入库单
     */
    @PostMapping("/audit_othercol_bill")
    @VtuzxApi( fields = {
            @VtuzxValidator(key = "billNo", label = "单号", notNull = true)
    } )
    public Object auditOtherColBill(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws VtuzxException {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String billNo = VtuzxUtil.getString(param, "billNo");
        String name = VtuzxUtil.getString(token, "name");
        return otherService.auditOtherColBill(_userId, name, billNo);
    }

    /**
     * 反审核其他入库单
     */
    @PostMapping("/unaudit_othercol_bill")
    @VtuzxApi( fields = {
            @VtuzxValidator(key = "billNo", label = "单号", notNull = true)
    } )
    public Object unauditOtherColBill(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws VtuzxException {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String billNo = VtuzxUtil.getString(param, "billNo");
        return otherService.unauditOtherColBill(_userId, billNo);
    }

    /**
     * 根据RFID从其他入库单中获取物料
     */
    @PostMapping("/get_material_by_rfid")
    @VtuzxApi( fields = {
            @VtuzxValidator(key = "rfid", label = "RFID", notNull = true)
    } )
    public Object getMaterialByRfid(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) {
        List<String> rfidList = VtuzxUtil.getObject(param, "rfid");
        return otherService.getOtherColMaterialByRfid(rfidList);
    }

    /**
     * 根据箱号从其他入库单中获取物料
     */
    @PostMapping("/get_material_by_box")
    @VtuzxApi( fields = {
            @VtuzxValidator(key = "boxNo", label = "箱号", notNull = true)
    } )
    public Object getMaterialByBox(@RequestBody Map<String, Object> param) throws VtuzxException {
        String boxNo = VtuzxUtil.getString(param, "boxNo");
//        String boxNo;
//        if (boxNoKey.contains("|;")) {
//            String[] boxValues = boxNoKey.split("\\|\\;");
//            if (boxValues.length == 5) {
//                String supplierNo = boxValues[0];
//                String materialNo = boxValues[1];
//                String batchNo = boxValues[2];
//                String flowNo = boxValues[3];
//                int count = Integer.parseInt(boxValues[4]);
//                return otherService.getOtherColMaterial(boxNoKey, supplierNo, materialNo, batchNo, flowNo, count);
//            }
//            boxNo = boxValues[boxValues.length - 1];
//        } else {
//            boxNo = boxNoKey;
//        }
        return otherService.getOtherColMaterial(boxNo);
    }

    /**
     * 获取其他入库单
     */
    @PostMapping("/get_othercol_bill")
    @VtuzxApi
    public Object getOtherColBill(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        int pageIndex = VtuzxUtil.getInt(param, IVtuzxConst.Key_pageIndex);
        int pageSize = VtuzxUtil.getInt(param, IVtuzxConst.Key_pageSize, 10);
        return otherService.getOtherColBill(_userId, param, pageIndex, pageSize);
    }

    /**
     * 获取其他入库单详细
     */
    @PostMapping("/get_othercol_bill_detail")
    @VtuzxApi( fields = {
            @VtuzxValidator(key = "billNo", label = "单号", notNull = true)
    } )
    public Object getOtherColBillDetail(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws VtuzxException {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String billNo = VtuzxUtil.getString(param, "billNo");
        return otherService.getOtherColBillDetail(_userId, billNo);
    }

    /**
     * 下载其他入库单列表(用于打印二维码)
     */
    @PostMapping("/download_othercol_bill_barcode")
    @VtuzxApi(type = ApiType.Download, fields = {
            @VtuzxValidator(key = "billNo", label = "单号", notNull = true)
    } )
    public Object downloadOtherColBillBarcode(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws Exception {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String billNo = VtuzxUtil.getString(param, "billNo");
        return otherService.downloadOtherColBillBarcode(_userId, billNo);
    }

    /**
     * 下载其他入库单列表(多单据, 用于打印二维码)
     */
    @PostMapping("/download_othercol_bill_barcode_list")
    @VtuzxApi(type = ApiType.Download, fields = {
            @VtuzxValidator(key = "billNoList", label = "单号列表", notNull = true)
    } )
    public Object downloadOtherColBillBarcodeList(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws Exception {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        List<String> billNoList = VtuzxUtil.getObject(param, "billNoList");
        return otherService.downloadOtherColBillBarcodeList(_userId, billNoList);
    }


}
